#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <unistd.h>

#include "iouring_lib.h"

int main(int argc, char** argv)
{
    IOURING_HANDLE hIOURing = IOURING_Create(1);
    if (hIOURing == IOURING_INVALID_HANDLE)
    {
        printf("IOURING_Create error. %s\n", strerror(errno));
        return -1;
    }

    int iErr = IOURING_Write(hIOURing, STDOUT_FILENO, "hello io_uring\n");
    if (iErr == -1)
    {
        printf("IOURING_Write error. %s\n", strerror(errno));
        IOURING_Destroy(hIOURing);
        return -1;
    }

    char szBuff[1024];
    iErr = IOURING_Read(hIOURing, STDIN_FILENO, szBuff, 1024);
    if (iErr == -1)
    {
        printf("IOURING_Read error. %s\n", strerror(errno));
        IOURING_Destroy(hIOURing);
        return -1;
    }

    IOURING_Schedule(hIOURing);

    iErr = IOURING_ReadFromCQ(hIOURing);
    if (iErr < 0)
    {
        printf("IOURING_ReadFromCQ error. %s\n", strerror(errno));
        IOURING_Destroy(hIOURing);
        return -1;
    }
    
    iErr = IOURING_ReadFromCQ(hIOURing);
    if (iErr < 0)
    {
        printf("IOURING_ReadFromCQ error. %s\n", strerror(errno));
        IOURING_Destroy(hIOURing);
        return -1;
    }

    printf("Read: %s\n", szBuff);

    IOURING_Destroy(hIOURing);

    return 0;
}